package com.usian.controller;

import com.usian.PageResult;
import com.usian.Result;
import com.usian.feign.ContentFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/backend/content")
public class ContentController {

    @Autowired
    private ContentFeign contentFeign;

    /***
     * 内容分类管理根据父节点查询子节点
     *
     * @param id
     * @return
     */
    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id){
        List<TbContentCategory> categoryList = contentFeign.selectContentCategoryByParentId(id);
        if(categoryList.size()>0){
            return Result.ok(categoryList);
        }
        return Result.error("查无数据");
    }

    /**
     * 添加内容节点
     *
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(Long parentId,String name ){
        Integer num= contentFeign.insertContentCategory(parentId,name);
        if(num>0){
            return Result.ok("");
        }
        return  Result.error("保存失败");
    }


    /**
     * 删除节点
     *
     * @param categoryId
     * @return
     */
    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(Long categoryId){
        Integer num= contentFeign.deleteContentCategoryById(categoryId);
        if(num == 200){
            return Result.ok("");
        }
        return  Result.error("删除失败");
    }

    /**
     * 根据分类id查询内容列表
     *
     * @param categoryId
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam Long categoryId,
                                                     @RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "10") Integer rows){
        PageResult pageResult = contentFeign.selectTbContentAllByCategoryId(categoryId, page, rows);
        if(pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查询失败");
    }

    /**
     * 增加分类内容
     *
     * @param tbContent
     * @return
     */
    @RequestMapping("insertTbContent")
    public  Result  insertTbContent(TbContent tbContent){
        int result = contentFeign.insertTbContent(tbContent);
        if(result>0){
            return Result.ok("保存成功");
        }
        return Result.error("保存失败");
    }

    /**
     * 根据id删除内容信息
     *
     * @param ids
     * @return
     */
    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(@RequestParam Long ids){
        int result = contentFeign.deleteContentByIds(ids);
        if(result>0){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
